//
//  HomeCamerView.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/9.
//

#import "HomeCamerView.h"
#import "DJCameraManager.h"
#import "PhotoManager.h"

@interface HomeCamerView()<DJCameraManagerDelegate>
@property (nonatomic,strong)DJCameraManager *manager;
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UIView * infoView;
@property (nonatomic,strong) UIImageView * iconView;
@property (nonatomic,strong) UIImageView * bgView;
@property (nonatomic,strong) UIButton * enterBtn;
@property (nonatomic,strong) ReLayoutButton * addBtn;
@property (nonatomic,strong) ReLayoutButton * filpBtn;

@property (nonatomic,strong) UIButton * againBtn;
@property (nonatomic,strong) UIButton * updateBtn;
@property (nonatomic, assign) BOOL isSessionRunning;


@end

@implementation HomeCamerView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        [self initLayout];
    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.nameLabel];
    [self addSubview:self.infoView];
    [self addSubview:self.iconView];
    [self addSubview:self.bgView];
    [self addSubview:self.enterBtn];
    [self addSubview:self.addBtn];
    [self addSubview:self.filpBtn];
    [self addSubview:self.againBtn];
    [self addSubview:self.updateBtn];
}

- (void)initLayout
{
    
    // 传入View的frame 就是摄像的范围
    DJCameraManager *manager = [[DJCameraManager alloc] initWithParentView:self.infoView];
    manager.delegate = self;
    manager.canFaceRecognition = YES;
    [manager setFaceRecognitonCallBack:^(CGRect faceFrame) {
        NSLog(@"你的脸在%@",NSStringFromCGRect(faceFrame));
    }];
    
    self.manager = manager;
}

-(void)startCamer{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager.session startRunning];
        self.isSessionRunning = YES;
       
       });
}

-(void)stopCamer{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.manager.session stopRunning];
           NSLog(@"Capture session stopped.");
           self.isSessionRunning = NO;
       });
}

-(void)enterBtnAction{
    @WeakObj(self)
    [self.manager takePhotoWithImageBlock:^(UIImage *originImage, UIImage *scaledImage, UIImage *croppedImage) {
        @StrongObj(self)
        if (croppedImage) {
            [self stopCamer];
            [self setBtnStyle:YES];
            self.iconView.image = croppedImage;
        }
    }];
}

-(void)filpBtnAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    [self.manager switchCamera:sender.selected didFinishChanceBlock:^{
            
    }];
}

-(void)addBtnAction{
    @WeakObj(self)
    [[PhotoManager sharedInstance] takePhoto:[UIViewController currentViewController] finished:^(HXPhotoModel * _Nonnull model) {
        @StrongObj(self)
        [self stopCamer];
        [self setBtnStyle:YES];
        self.iconView.image = model.previewPhoto;
        
    }];
}

-(void)againBtnAction{
    
    if (self.isSessionRunning) {
        [self stopCamer];
    } else {
        [self startCamer];
        [self setBtnStyle:NO];
    }
}

-(void)updateBtnAction{
    [self stopCamer];
    @WeakObj(self)
    FLodingStr(@"正在上传")
    [SPNetworkingIns updateIdImage:self.iconView.image url:PORT(@"sleep-center/api/ringSleepData/uploadFiles") name:@"file"  success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        FHideLoding
        NSString * imagStr = K_TimeJson(responseObject[@"data"], @"fileUrl");
        if (self.delegate && [self.delegate respondsToSelector:@selector(getImageUrl:)]) {
            [self.delegate getImageUrl:imagStr];
        }
        
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
        
}

-(void)setBtnStyle:(BOOL)isHide{
    
    if (isHide) {
        self.enterBtn.hidden = isHide;
        self.addBtn.hidden = isHide;
        self.filpBtn.hidden = isHide;
        self.againBtn.hidden = !isHide;
        self.updateBtn.hidden = !isHide;
        self.iconView.hidden = !isHide;
    }else{
        self.enterBtn.hidden = isHide;
        self.addBtn.hidden = isHide;
        self.filpBtn.hidden = isHide;
        self.againBtn.hidden = !isHide;
        self.updateBtn.hidden = !isHide;
        self.iconView.hidden = !isHide;
    }
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake((WIDTH - 320 * FMScaleX) / 2 , 20 * FMScaleX, 320 * FMScaleX, 20 * FMScaleX)];
        _nameLabel.font = Regular(13);
        _nameLabel.textColor = RDRGB(0xAD7D62);
        _nameLabel.text = @"舌体放松，舌面平展，舌尖略向下，口张大不要太用力";
    }
    return _nameLabel;
}

-(UIView *)infoView{
    
    if (!_infoView) {
        _infoView = [[UIView alloc]initWithFrame:CGRectMake(0, GETMaxY(self.nameLabel) + 10 * FMScaleX, WIDTH, WIDTH)];
        _infoView.backgroundColor = UIColor.clearColor;
    }
    return _infoView;
}

-(UIImageView *)iconView{
    
    if (!_iconView) {
        _iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, GETMaxY(self.nameLabel) + 10 * FMScaleX, WIDTH, WIDTH)];
        _iconView.hidden = YES;
    }
    return _iconView;
}

-(UIImageView *)bgView{
    
    if (!_bgView) {
        _bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, GETMaxY(self.nameLabel) + 10 * FMScaleX, WIDTH, WIDTH)];
        _bgView.image = [UIImage imageNamed:@"Home_all_addicon"];
    }
    return _bgView;
}
-(UIButton *)enterBtn{
    
    if (!_enterBtn) {
        _enterBtn = [[UIButton alloc]initWithFrame:CGRectMake((WIDTH - 80 * FMScaleX) / 2, self.getHeight - 120 * FMScaleX, 80 * FMScaleX, 80 * FMScaleX)];
        [_enterBtn setBackgroundImage:[UIImage imageNamed:@"Home_all_addbtn"] forState:UIControlStateNormal];
        [_enterBtn setTitle:@"拍摄" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _enterBtn.titleLabel.font = Regular(14);
        [_enterBtn addTarget:self action:@selector(enterBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterBtn;
}
-(ReLayoutButton *)addBtn{
    
    if (!_addBtn) {
        _addBtn = [[ReLayoutButton alloc]initWithFrame:CGRectMake(40 * FMScaleX, GETMinY(self.enterBtn) + 12 * FMScaleX, 65 * FMScaleX, 70 * FMScaleX)];
        [_addBtn setTitle:@"相册上传" forState:UIControlStateNormal];
        [_addBtn setImage:[UIImage imageNamed:@"Home_all_addphoto"] forState:UIControlStateNormal];

        _addBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _addBtn.layoutType = RelayoutTypeUpDown;
        _addBtn.margin = 6;
        [_addBtn setTitleColor:RDRGB(0x714933) forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.titleLabel.font = Regular(14);
    }
    return _addBtn;
}

-(ReLayoutButton *)filpBtn{
    
    if (!_filpBtn) {
        _filpBtn = [[ReLayoutButton alloc]initWithFrame:CGRectMake(WIDTH - 105 * FMScaleX, GETMinY(self.enterBtn) + 12 * FMScaleX, 65 * FMScaleX, 70 * FMScaleX)];
        [_filpBtn setTitle:@"翻转相机" forState:UIControlStateNormal];
        [_filpBtn setImage:[UIImage imageNamed:@"Home_all_addfilp"] forState:UIControlStateNormal];

        _filpBtn.imageView.frame = CGRectMake(0, 0, 40 * FMScaleX, 40 * FMScaleX);
        _filpBtn.layoutType = RelayoutTypeUpDown;
        _filpBtn.margin = 6;
        [_filpBtn setTitleColor:RDRGB(0x714933) forState:UIControlStateNormal];
        [_filpBtn addTarget:self action:@selector(filpBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _filpBtn.titleLabel.font = Regular(14);
    }
    return _filpBtn;
}

-(UIButton *)againBtn{
    
    if (!_againBtn) {
        _againBtn = [[UIButton alloc]initWithFrame:CGRectMake(40 * FMScaleX , self.getHeight - 120 * FMScaleX, 130 * FMScaleX, 40 * FMScaleX)];
        [_againBtn setBackgroundImage:[UIImage imageNamed:@"Home_all_check_btn"] forState:UIControlStateNormal];
        [_againBtn setTitle:@"重新拍照" forState:UIControlStateNormal];
        [_againBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_againBtn addTarget:self action:@selector(againBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _againBtn.hidden = YES;
    }
    return _againBtn;
}
-(UIButton *)updateBtn{
    
    if (!_updateBtn) {
        _updateBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH - 170 * FMScaleX , self.getHeight - 120 * FMScaleX, 130 * FMScaleX, 40 * FMScaleX)];
        [_updateBtn setBackgroundImage:[UIImage imageNamed:@"Home_all_check_btn"] forState:UIControlStateNormal];
        [_updateBtn setTitle:@"确认上传" forState:UIControlStateNormal];
        [_updateBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_updateBtn addTarget:self action:@selector(updateBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _updateBtn.hidden = YES;
    }
    return _updateBtn;
}
@end
